//
//  SupplierManager.m
//  takeout
//
//  Created by Derekc on 12-2-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SupplierManager.h"
#import "ResolveXML.h"
#import "Constants.h"

@implementation SupplierManager
-(NSArray *)getAllSupplierByCategoryId:(NSString *)suppierCategoryId{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_SUPPLIERS_BY_CATEGORIES,suppierCategoryId]];
    return [self getSupplierByUrl:URIString];
}
-(NSArray *)getAllSupplier{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN,SERVICE_ALL_SUPPLIERS];
    return [self getSupplierByUrl:URIString];
}


-(Supplier *)getSupplierDetailById:(NSString *) supId{
    ResolveXML *resolveXML=[[ResolveXML alloc]init];
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_SEARCH_SUPPLIER_BY_ID, supId]];
    GDataXMLDocument *doc=[resolveXML getXMLDocument:URIString];
    NSArray *children = [doc.rootElement children]; 
    Supplier *supplier = [[Supplier alloc] init];
    
    supplier.supplierId = [[[(GDataXMLElement*)[children objectAtIndex:0] childAtIndex:0] stringValue] intValue];
    supplier.supplierName = [[(GDataXMLElement*)[children objectAtIndex:1] childAtIndex:0] stringValue];
    supplier.address = [[(GDataXMLElement*)[children objectAtIndex:2] childAtIndex:0] stringValue];
    supplier.road = [[(GDataXMLElement*)[children objectAtIndex:3] childAtIndex:0] stringValue];
    supplier.mapAddress = [[(GDataXMLElement*)[children objectAtIndex:4] childAtIndex:0] stringValue];
    supplier.longitude = [[[(GDataXMLElement*)[children objectAtIndex:5] childAtIndex:0] stringValue] floatValue];
    supplier.latitude = [[[(GDataXMLElement*)[children objectAtIndex:6] childAtIndex:0] stringValue] floatValue];
    supplier.phone = [[(GDataXMLElement*)[children objectAtIndex:7] childAtIndex:0] stringValue];
    supplier.phone2 = [[(GDataXMLElement*)[children objectAtIndex:8] childAtIndex:0] stringValue];
    supplier.telephone = [[(GDataXMLElement*)[children objectAtIndex:9] childAtIndex:0] stringValue];
    supplier.telephone2 = [[(GDataXMLElement*)[children objectAtIndex:10] childAtIndex:0] stringValue];
    supplier.joinTime = [[(GDataXMLElement*)[children objectAtIndex:11] childAtIndex:0] stringValue];
    supplier.startTime = [[[(GDataXMLElement*)[children objectAtIndex:12] childAtIndex:0] stringValue] integerValue];
    supplier.endTime = [[[(GDataXMLElement*)[children objectAtIndex:13] childAtIndex:0] stringValue] integerValue];
    supplier.leastPrice = [[[(GDataXMLElement*)[children objectAtIndex:14] childAtIndex:0] stringValue] floatValue];
    supplier.leastDistance = [[[(GDataXMLElement*)[children objectAtIndex:15] childAtIndex:0] stringValue] floatValue];
    supplier.deliveryDesc = [[(GDataXMLElement*)[children objectAtIndex:16] childAtIndex:0] stringValue];
    supplier.avgPrice =  [[[(GDataXMLElement*)[children objectAtIndex:17] childAtIndex:0] stringValue] floatValue];
    supplier.consumerecord = [[[(GDataXMLElement*)[children objectAtIndex:18] childAtIndex:0] stringValue] integerValue];
    supplier.imgUrl = [[(GDataXMLElement*)[children objectAtIndex:19] childAtIndex:0] stringValue];
    supplier.desc = [[(GDataXMLElement*)[children objectAtIndex:20] childAtIndex:0] stringValue];
    supplier.supCateId = [[[(GDataXMLElement*)[children objectAtIndex:21] childAtIndex:0] stringValue] integerValue];
    supplier.city = [[[(GDataXMLElement*)[children objectAtIndex:22] childAtIndex:0] stringValue] integerValue];
    supplier.modifyDate = [[(GDataXMLElement*)[children objectAtIndex:23] childAtIndex:0] stringValue];
    supplier.supplierCategoryName = [[(GDataXMLElement*)[children objectAtIndex:24] childAtIndex:0] stringValue];    
    NSLog(@"%@",supplier.supplierCategoryName);
    
    [supplier release];
    [resolveXML release];
    return supplier;
}

-(NSArray *) getSupplierByUrl:(NSString *)URIString{
    ResolveXML *resolveXML=[[ResolveXML alloc]init];
    GDataXMLDocument *doc=[resolveXML getXMLDocument:URIString];
    NSArray *children = [doc.rootElement children]; 
    NSMutableArray *result = nil;
    for (GDataXMLElement *element in children) {
        if ([element isKindOfClass:[GDataXMLElement class]]) {
            Supplier *supplier=[[Supplier alloc] init];
            supplier.supplierId=[[[element childAtIndex:0] stringValue] intValue];
            supplier.supplierName=[[element childAtIndex:1] stringValue];
            supplier.address=[[element childAtIndex:2] stringValue];
            supplier.road=[[element childAtIndex:3] stringValue];
            supplier.mapAddress=[[element childAtIndex:4] stringValue];
            supplier.longitude=[[[element childAtIndex:5] stringValue] floatValue];
            supplier.latitude=[[[element childAtIndex:6] stringValue] floatValue];
            supplier.phone=[[element childAtIndex:7] stringValue];
            supplier.phone2=[[element childAtIndex:8] stringValue];
            supplier.telephone=[[element childAtIndex:9] stringValue];
            supplier.telephone2=[[element childAtIndex:10] stringValue];
            supplier.joinTime=[[element childAtIndex:11] stringValue];
            supplier.startTime=[[[element childAtIndex:12] stringValue] intValue];
            supplier.endTime=[[[element childAtIndex:13] stringValue] intValue];
            supplier.leastPrice=[[[element childAtIndex:14] stringValue] floatValue];
            supplier.leastDistance=[[[element childAtIndex:15] stringValue] floatValue];
            supplier.deliveryDesc=[[element childAtIndex:16] stringValue];
            supplier.avgPrice=[[[element childAtIndex:17] stringValue] floatValue];
            supplier.consumerecord=[[[element childAtIndex:18] stringValue] intValue];
            supplier.imgUrl=[[element childAtIndex:19] stringValue];
            supplier.desc=[[element childAtIndex:20] stringValue];
            supplier.supCateId=[[[element childAtIndex:21] stringValue] intValue];
            supplier.city=[[[element childAtIndex:22] stringValue] intValue];
            supplier.modifyDate = [[element childAtIndex:23] stringValue];
            supplier.supplierCategoryName = [[element childAtIndex:24] stringValue];
            //            supplier.status=nil;
            if (result == nil) {
                result = [NSMutableArray arrayWithObject:supplier];
            }else{
                [result addObject:supplier];
            }
            [supplier release];
        }
    } 
    [resolveXML release];
    return result;

}

-(NSArray *) getSupplier:(NSString *)supplierCategoryId pageIndex:(NSInteger) pageIndex road:(NSString *) road isSelling:(NSString *) isSelling{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_SUPPLIERS_BY_CATEGORIES,supplierCategoryId]];
    URIString = [URIString stringByAppendingFormat:@"&%@&%@",[NSString stringWithFormat: PAGEINDEX,pageIndex],PAGESIZE]; 
    if(road != nil){
        URIString = [URIString stringByAppendingFormat:@"&%@",[NSString stringWithFormat: ROAD,road]]; 
    }
    if(isSelling != nil){
        URIString = [URIString stringByAppendingFormat:@"&%@",[NSString stringWithFormat: ISSELLING,isSelling]]; 
    }
    CUSTOM_LOG(URIString);
    return [self getSupplierByUrl:URIString];
}

@end
